/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 25, 2012  2:52:52 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : sap.business.mm.outbound
 * File Name : MmGpOutbService.java
 * TODO
 */
package sap.business.mm.outbound;

import java.util.ArrayList;
import java.util.List;
import jpa.entitys.synchro.staging.MmGpOutb;
import jpa.service.synchro.staging.MmGpOutbServices;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import sap.business.commons.SynchroOutAbstract;
import sap.jco.client.Connection;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;

/**
 * @author chatree
 *
 */
public class MmGpOutbService extends SynchroOutAbstract {
	
	private static final Logger log = Logger.getLogger(MmGpOutbService.class);
	private static MmGpOutbServices MmGpOutbServ = null; 
	
	/**
	 * Date =Nov 12, 2013 : 2:29:59 PM 
	 * CreateBy=Chatree Siangklom
	 * MmGpOutbService
	 * TODO
	 * @throws Exception
	 */
	public MmGpOutbService() throws Exception {
		try{
			
			super.emSynchro = super.CreateEmSynchro();
			
			super.emPromis = super.CreateEmSynchro();
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	/**
	 * (Javadoc)
	 * @see sap.business.commons.SynchroOutAbstract#SynchroToSap(com.sap.conn.jco.JCoFunction, com.sap.conn.jco.JCoTable, com.sap.conn.jco.JCoStructure, java.util.List)
	 * Date =Nov 12, 2013 : 2:29:56 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	@Override
	public void SynchroToSap(JCoFunction jcoFunction, JCoTable jcoTable, JCoStructure jCoStructure, List<?> value_list) throws Exception {

		try{
			
			//Check Is not isEmpty is exit method.
			if(!jcoTable.isEmpty())
			{
				log.info("Bapi = "+ sap_bapi +", table "+ sap_table +" is not Empty ");
				return;
			}	
			
			if(!CommonService.IsNull(value_list)){
				jCoStructure.clear();
				log.info("value_list.size : " + value_list.size());			
				log.info("table.getNumRows : " + jcoTable.getNumRows());
				log.info("table.getNumColumns : " + jcoTable.getNumColumns());
				
				//Header
				MmGpOutb model = (MmGpOutb)value_list.get(0);
				jCoStructure.setValue("GRN_NUMBER", model.getPromisGpDocNo());
				//jCoStructure.setValue("GP_TYPE", "76");
				jCoStructure.setValue("BKTXT", model.getRemarks());
				jCoStructure.setValue("XBLNR", model.getSupplierDoNo());
				jCoStructure.setValue("BLDAT", model.getSupplierDoDate());
				jCoStructure.setValue("BUDAT", model.getGpDate());
				jCoStructure.setValue("BWART", model.getStatus());
				
				jcoTable.deleteAllRows();
				for (Object obj : value_list) 
		        {
					model = (MmGpOutb)obj;
					log.info("InstanceNo = " + model.getInstanceNo() + " -- PROMIS_TXN_ID = " +  model.getPromisStagingId());
					jcoTable.appendRow();
					jcoTable.setValue("PROMIS_TXN_ID", model.getPromisStagingId());
					
					jcoTable.setValue("EBELN", model.getExternalPoNo());
					jcoTable.setValue("EBELP", model.getExternalPoLineItem());
					jcoTable.setValue("MATNR", model.getItemCode());
					jcoTable.setValue("WERKS", model.getSapPlantCode());
					jcoTable.setValue("MENGE", "0");
					jcoTable.setValue("ERFME", model.getItemUm());
					jcoTable.setValue("WEMPF", model.getPreparedBy());
					jcoTable.setValue("SGTXT", model.getArticelDescription());
					jcoTable.setValue("DEL_IND", "2");
		        }
			}
					
			super.SynchroToSap(jcoFunction, jcoTable, jCoStructure, value_list);
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	@Override
	public void CallbackMessageFromSap(JCoTable jcoTable_return) throws Exception {
		// TODO Auto-generated method stub
		
		try{
			
			List<MmGpOutb> lst = new ArrayList<MmGpOutb>();
			if(jcoTable_return != null && jcoTable_return.isEmpty() == false)
			{
				log.info("Update Message table Retrun getNumRows = " + jcoTable_return.getNumRows());
				for(int i = 0; i < jcoTable_return.getNumRows(); i++)
				{
					jcoTable_return.setRow(i);
					MmGpOutb model = MmGpOutbServ.findById(CommonService.IsNullRNull(jcoTable_return.getString("PROMIS_TXN_ID")));
					log.info("PROMIS_TXN_ID = " + jcoTable_return.getString("PROMIS_TXN_ID"));
					
					if(model != null){
						
						model.setMsgCode(CommonService.IsNullRNull(jcoTable_return.getString("MSG_CODE")));
						model.setMsgDesc(CommonService.IsNullRNull(jcoTable_return.getString("MSG_DESC")));
						model.setMsgAltDesc(CommonService.IsNullRNull(jcoTable_return.getString("MSG_ALT_DESC")));
						model.setMsgActResult(CommonService.IsNullRNull(jcoTable_return.getString("MSG_ACTION_RESULT")));
						model.setIsactive("Y");
						model.setIssync("Y");
						
						lst.add(model);
					}
				}
				
				MmGpOutbServ.Updated(lst);
			}
			
			super.CallbackMessageFromSap(jcoTable_return);
			
		}catch(Exception e){
			
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	
	public void Start() throws Exception
	{
		this.sap_bapi = "ZIN_MMPUR_GR";
		this.sap_structure = "GI_HEAD";
		this.sap_table = "GT_ITEM";
		this.sap_table_return = "RETURN";
		this.jco_conn  = new Connection();
		MmGpOutbServ = new MmGpOutbServices(emSynchro);
		
		//Get SAP BAPI function
		JCoFunction jcoFunction = jco_conn.getJCoFunction(sap_bapi);
		
		//Get SAP Staging table
		JCoTable jcoTable = jco_conn.getTables(sap_table);
		JCoStructure jCoStructure = jco_conn.getImportStructure(sap_structure);

		List<MmGpOutb> value_list = MmGpOutbServ.findAllIsSync("Y");
		
		SynchroToSap(jcoFunction, jcoTable, jCoStructure, value_list);
		
		//Get SAP Staging table retrun message
		JCoTable jcoTable_return = jco_conn.getTables(sap_table_return);
		CallbackMessageFromSap(jcoTable_return);
		
	}
	
//	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub		
//		try{
//			
//			MmGpOutbService obj = new MmGpOutbService();	
//			
//			obj.Start();
//			
//		}catch(Exception e)
//		{
//			log.error(e);
//			e.printStackTrace();			
//		}
//	}

}
